#include <iostream>
// #include "stdio.h"

using namespace std;

struct mystr
{
    unsigned char x0 : 1;
    unsigned char x1 : 1;
    unsigned char x2 : 1;
    unsigned char x3 : 1;
    unsigned char x4 : 1;
    unsigned char x5 : 1;
    unsigned char x6 : 1;
    unsigned char x7 : 1; // 存储高位
};

union myun
{
    struct mystr cn;
    unsigned char s;
};

union myun tmp;

int main()
{
    tmp.s = 174; //二进制形式是10101110

    printf("tmp.s is %d\n", tmp.s);

    cout << "sizeof(tmp) = " << sizeof(tmp) << endl;

    printf("x0 is %d\n", tmp.cn.x0);
    printf("x1 is %d\n", tmp.cn.x1);
    printf("x2 is %d\n", tmp.cn.x2);
    printf("x3 is %d\n", tmp.cn.x3);
    printf("x4 is %d\n", tmp.cn.x4);
    printf("x5 is %d\n", tmp.cn.x5);
    printf("x6 is %d\n", tmp.cn.x6);
    printf("x7 is %d\n", tmp.cn.x7);

    cout << "x7x6x5x4x3x2x1x0 " << (int)tmp.cn.x7
                                << (int)tmp.cn.x6
                                << (int)tmp.cn.x5
                                << (int)tmp.cn.x4
                                << (int)tmp.cn.x3
                                << (int)tmp.cn.x2
                                << (int)tmp.cn.x1
                                << (int)tmp.cn.x0
                                << endl;

    // cn change, s change 
    tmp.cn.x0 = 1;
    cout << "after x0 changed, tmp.s = " << int(tmp.s) << endl;

    return 0;
}